package com.yixueyun.media.api;

import com.yixueyun.base.model.PageParams;
import com.yixueyun.base.model.PageResult;
import com.yixueyun.media.model.dto.QueryMediaParamsDto;
import com.yixueyun.media.model.dto.UploadFileParamsDto;
import com.yixueyun.media.model.dto.UploadFileResultDto;
import com.yixueyun.media.model.po.MediaFiles;
import com.yixueyun.media.service.MediaFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * @author kklmars
 * @version 1.0
 * @description 媒资文件管理接口
 * @date
 */
@Api(value = "媒资文件管理接口", tags = "媒资文件管理接口")
@RestController
public class MediaFilesController {


    @Autowired
    MediaFileService mediaFileService;


    @ApiOperation("媒资列表查询接口")
    @PostMapping("/files")
    public PageResult<MediaFiles> list(PageParams pageParams, @RequestBody QueryMediaParamsDto queryMediaParamsDto) {
        Long companyId = 1232141425L;
        return mediaFileService.queryMediaFiels(companyId, pageParams, queryMediaParamsDto);

    }
    @ApiOperation("上传图片接口")
    @RequestMapping(value = "/upload/coursefile",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @ResponseBody
    public UploadFileResultDto upload(@RequestPart("filedata")MultipartFile filedata,
                                      @RequestParam(value = "objectname",required = true) String objectName )throws IOException {
        //机构id
        Long companyId = 1232141425L;
        //构建 UploadFileResultDto
        UploadFileParamsDto uploadFileParamsDto = new UploadFileParamsDto();
        //文件类型
        uploadFileParamsDto.setFileType("001001");
        //文件名
        uploadFileParamsDto.setFilename(filedata.getOriginalFilename());
        //文件大小
        uploadFileParamsDto.setFileSize(filedata.getSize());

        //构建 filepath
        File tempFile = File.createTempFile("minio", ".temp");
        filedata.transferTo(tempFile);
        String filePath = tempFile.getAbsolutePath();
        UploadFileResultDto upload = mediaFileService.upload(companyId, uploadFileParamsDto, filePath,objectName);
        return upload;
    }

}
